﻿Ext.override(Ext.form.Action.Load, {
    onSuccess: function (response) {
        var result = this.processResponse(response), form = this.form;
        if (result === true || !result.success || !result.data) {
            this.failureType = Ext.form.action.Action.LOAD_FAILURE;
            form.afterAction(this, false);
            return;
        }
        //form.beforeSetafterLoad(result.data.data); // zhangxian添加
        form.clearInvalid();
        //Kwan.Debug(result.data.data[0]);
        form.setValues(result.data.data);
        form.afterAction(this, true);
    }
});
Ext.override(Ext.form.BasicForm, {
    beforeSetafterLoad: function (data) {
        this.fireEvent('beforeSetafterLoad', data)
    },
    setValues: function (values) {
        if (!values) return;
        var me = this;
        //Kwan.Debug(values);
        function setVal(fieldId, val) {
            // ---zhangxian添加 解决给combo自动赋值的问题
            if (Ext.isObject(val)) {
                var field = me.findField(fieldId + '.EntityID');
                Kwan.Debug(me.getFields());
                if (field) {
                    if (field.store.count() == 0) {// 如果是remote加载的combo
                        field.store.add({
                            Id: val.Id,
                            Text: val.Text
                        })
                    }

                    field.setValue(val.Id);
                    if (me.trackResetOnLoad) {
                        field.resetOriginalValue();
                    }
                }
            } else if (Ext.isArray(val)) {// 如果是多选的combo
                var field = me.findField(fieldId + '.EntityID');
                if (field) {
                    if (field.store.count() == 0) {
                        Ext.each(val, function (vItem) {
                            field.store.add({
                                Id: vItem.Id,
                                Text: vItem.Text
                            })
                        });
                    }
                    var values = '';
                    Ext.each(val, function (vItem) {
                        values = values + vItem.Id + ', '// 注意这个空格很诡异，没有空格不识别
                    });
                    field.setValue(values.substring(0, values.length
											- 2)); //去掉多余的', '
                }

            } else {
                var field = me.findField(fieldId);
                if (field) {
                    field.setValue(val);
                    if (me.trackResetOnLoad) {
                        field.resetOriginalValue();
                    }
                }
            }

        }
        Kwan.bindForm(values, me);
        //Ext.each(me._fields.items, function (field) {
        //    if (field) {
                //var value = values[0][field.name];
                //if (field.store) {
                //var display = field.store.data.items[value].data.Key;
                //if (field.name === 'Cid') {
                //    display = values[0]['ClassName'];
                //    field.valueNotFoundText = display;
                //} else {
                //    display = field.store.data.items[value].data.Key;
                //}
                //field.setRawValue(display);
                //field.setValue(value);
                //}
        //        field.setValue(values[0][field.name]);
        //    }
        //});
        return this;
    }
});